package cn.stylefeng.guns.modular.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.BusinessException;
import cn.stylefeng.guns.modular.business.entity.WxExpense;
import cn.stylefeng.guns.modular.business.exception.WxExpenseExceptionEnum;
import cn.stylefeng.guns.modular.business.mapper.WxExpenseMapper;
import cn.stylefeng.guns.modular.business.pojo.WxExpenseRequest;
import cn.stylefeng.guns.modular.business.service.WxExpenseService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 车辆管理业务实现层
 *
 * @author 施律名
 * @date 2022-03-10 17:48:59
 */
@Service
public class WxExpenseServicelmpl extends ServiceImpl<WxExpenseMapper, WxExpense> implements WxExpenseService {

    @Override
    public void add(WxExpenseRequest wxExpenseRequest) {
        WxExpense wxExpense = new WxExpense();
        BeanUtil.copyProperties(wxExpenseRequest, wxExpense);
        this.save(wxExpense);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void del(WxExpenseRequest wxExpenseRequest) {
        WxExpense wxExpense = this.queryWxExpense(wxExpenseRequest);
        this.removeById(wxExpense.getWxExpenseId());
    }

    @Override
    public void edit(WxExpenseRequest wxExpenseRequest) {
        WxExpense wxExpense = this.queryWxExpense(wxExpenseRequest);
        BeanUtil.copyProperties(wxExpenseRequest, wxExpense);
        this.updateById(wxExpense);
    }

    @Override
    public WxExpense detail(WxExpenseRequest wxExpenseRequest) {
        return this.queryWxExpense(wxExpenseRequest);
    }

    @Override
    public PageResult<WxExpense> findPage(WxExpenseRequest wxExpenseRequest) {
        LambdaQueryWrapper<WxExpense> wrapper = createWrapper(wxExpenseRequest);
        Page<WxExpense> sysRolePage = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(sysRolePage);
    }

    @Override
    public List<WxExpense> findList(WxExpenseRequest wxExpenseRequest) {
        LambdaQueryWrapper<WxExpense> wrapper = this.createWrapper(wxExpenseRequest);
        return this.list(wrapper);
    }

    /**
     * 获取车辆信息
     *
     * @author 施律名
     * @date 2022-03-10 17:48:59
     */
    private WxExpense queryWxExpense(WxExpenseRequest wxExpenseRequest) {
        WxExpense wxExpense = this.getById(wxExpenseRequest.getWxExpenseId());
        if (ObjectUtil.isEmpty(wxExpense)) {
            throw new BusinessException(WxExpenseExceptionEnum.WX_EXPENSE_NOT_EXISTED);
        }
        return wxExpense;
    }

    /**
     * 创建查询wrapper
     *
     * @author 施律名
     * @date 2022-03-10 17:48:59
     */
    private LambdaQueryWrapper<WxExpense> createWrapper(WxExpenseRequest wxExpenseRequest) {
        LambdaQueryWrapper<WxExpense> queryWrapper = new LambdaQueryWrapper<>();

        Long wxExpenseUserId = wxExpenseRequest.getWxExpenseUserId();
        queryWrapper.like(ObjectUtil.isNotEmpty(wxExpenseUserId), WxExpense::getWxExpenseUserId, wxExpenseUserId);

        // 根据时间倒序排列
        queryWrapper.orderByDesc(WxExpense::getCreateTime);

        return queryWrapper;
    }


}